package com.keyinong.caizhengting.myfragement;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 收货地址
 */

public class DizhiActivity extends Activity {
    /**
     * 网络请求
     */
    HttpTools httptools = null;
    String address;
    /**
     * 返回
     */
    private ImageButton ib_fanhui;
    /**
     * 确认
     */
    private Button btn_queren;
    /**
     * 姓名
     */
    private EditText et_name;
    /**
     * 联系电话
     */
    private EditText et_phone;
    /**
     * 地址
     */
    private EditText et_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dizhi);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_shoujihao3);
        et_address = (EditText) findViewById(R.id.et_dizhi);
        UserInfo();
        btn_queren = (Button) findViewById(R.id.et_queren);
        btn_queren.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Dizhi();

            }
        });
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui5);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void UserInfo() {
        SharedPreferences sp = getSharedPreferences("spReg", Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.person(token, new JsonUser());
    }

    private void Dizhi() {
        SharedPreferences sp = getSharedPreferences("spReg", Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String name = et_name.getText().toString();
        address = et_address.getText().toString();
        String phone = et_phone.getText().toString();
        if ("".equals(name)) {
            Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_LONG).show();
            return;
        } else if ("".equals(phone)) {
            Toast.makeText(getApplicationContext(), "请输入联系电话", Toast.LENGTH_LONG).show();
            return;
        } else if ("".equals(address) || "null".equals(address)) {
            Toast.makeText(getApplicationContext(), "请输入联系地址", Toast.LENGTH_LONG).show();
            return;
        }
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.dizhi(token, name, address, phone, new JsonDizhi());

    }

    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    private class JsonUser extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String name = object.getString("name");
                    String phone = object.getString("phone");
                    address = object.getString("address");
                    Log.i("zq", "name--" + name);
                    if (address.equals("null")) {
                        address = "";
                    }
                    et_name.setText(name);
                    et_phone.setText(phone);
                    et_address.setText(address);

                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private class JsonDizhi extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
